/**
 * @filename:StkweightController Jan 23, 2025
 * @project Wonder Vue Admin Api  1.1.0
 * Copyright(c) 2020 Wonder Co. Ltd. 
 * All right reserved. 
 */
package com.wonder.app.controller;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Date;
import java.util.Map;

import cn.hutool.json.JSONObject;
import com.vtuzx.core.bean.dto.VtuzxMap;
import com.wonder.app.domain.Stkweight;
import com.wonder.app.service.StkweightService;
import com.wonder.app.request.StkweightRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.wonder.common.common.AjaxMap;
import com.wonder.common.common.AjaxResult;
import com.wonder.common.common.CommonPage;
import com.wonder.common.utils.MessageUtils;
import com.wonder.common.utils.StringUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;

import javax.servlet.http.HttpServletRequest;

/**
 * 
 * <p>说明： 称重申请单API接口层</P>
 * @version: 1.1.0
 * @author: Wonder
 * @time    Jan 23, 2025
 *
 */
@Api(tags = "称重申请单")
@RestController
@RequestMapping("/app/Stkweight")
public class StkweightController {
	
	@Autowired
	private StkweightService stkweightService;
	
	@ApiOperation(value = "称重申请单列表")
	@RequestMapping(value = "/index", method = RequestMethod.POST)
	public Map<String,Object> index(@RequestBody StkweightRequest stkweightRequest) {
		Page<Stkweight> page = new Page<Stkweight>(stkweightRequest.getPage(), stkweightRequest.getLimit());
		IPage<Stkweight> list = stkweightService.selectList(stkweightRequest, page);
		
		Map<String,Object> retMap=new HashMap<>();
		retMap.put("data",CommonPage.restPage(list));
		
  
		return AjaxMap.success(retMap);
	}
	
	/**
	 * @explain 修改排序开关
	 * @param   entity
	 * @return  Boolean
	 * @author  Wonder
	 */
	@PostMapping("/updateExt")
	@ApiOperation(value = "修改排序开关", notes = "作者：Wonder")
	public AjaxResult<Stkweight> updateExt(@RequestBody Stkweight entity){
		if (null!=entity) {
			boolean rsg = stkweightService.updateById(entity);
			if (rsg) {
				  return AjaxResult.success("system.updateSuccess");
			  }else {
				  return AjaxResult.failed("system.updateFailed");
			  }
		}else {
			return AjaxResult.failed("system.parameterError");
		}
	}
	
	/**
	 * @explain 添加
	 * @param   entity
	 * @return  Boolean
	 * @author  Wonder
	 */
	@PostMapping("/add")
	@ApiOperation(value = "添加", notes = "作者：Wonder")
	public AjaxResult<Stkweight> add(@RequestBody Stkweight entity){

		if (null!=entity) {
		    
		    String validate=stkweightService.validate(entity,"new");
		    if(StringUtils.hasText(validate))
		         return AjaxResult.failed(validate);    
			boolean rsg = stkweightService.save(entity);
			if (rsg) {
				  return AjaxResult.success("system.insertSuccess");
			  }else {
				  return AjaxResult.failed("system.insertFailed");
			  }
		}else {
			return AjaxResult.failed("system.parameterError");
		}
	}
	
	/**
	 * @explain 查询对象  <swagger GET请求>
	 * @param   map 对象参数：id
	 * @return  JsonResult
	 * @author  Wonder
	 */
	@PostMapping("/getUpdateInfo")
	@ApiOperation(value = "获取称重申请单对象", notes = "作者：Wonder")
	public AjaxResult<Stkweight> getUpdateInfo(@RequestBody Map<String,Object> map){
	    if(map==null) return AjaxResult.failed("system.parameterError");
		Long Id=Long.parseLong(map.get("Id").toString());		
		 Stkweight stkweight=stkweightService.getById(Id);
		
		if (null!=stkweight ) {
			 return AjaxResult.success(stkweight);
		}else {
			 return AjaxResult.failed("system.parameterError");
		}
		
	}
	
	/**
	 * @explain 修改
	 * @param   entity
	 * @return  Boolean
	 * @author  Wonder
	 */
	@PostMapping("/update")
	@ApiOperation(value = "修改", notes = "作者：Wonder")
	public AjaxResult<Stkweight> update(@RequestBody Stkweight entity){
		if (null!=entity) {
		    String validate=stkweightService.validate(entity,"update");
		    if(StringUtils.hasText(validate))
		         return AjaxResult.failed(validate);
			boolean rsg = stkweightService.updateById(entity);
			if (rsg) {
				  return AjaxResult.success("system.updateSuccess");
			  }else {
				  return AjaxResult.failed("system.updateFailed");
			  }
		}else {
			return AjaxResult.failed("system.parameterError");
		}
	}
	
	/**
	 * @explain 删除
	 * @param   map 对象参数：id
	 * @return  JsonResult
	 * @author  Wonder
	 */
	@PostMapping("/delete")
	@ApiOperation(value = "删除", notes = "作者：Wonder")
	public AjaxResult<Stkweight> delete(@RequestBody Map<String,Object> map){
		if(map==null)return AjaxResult.failed("system.parameterError");
		String ids=map.get("Id").toString();
		ids=StringUtils.remove(ids, '[');
		ids=StringUtils.remove(ids, ']');
		String[] deleteIds=StringUtils.split(ids, ',');
		List<Long> idList=new ArrayList<>(); 
		for(int i=0;i<deleteIds.length;i++)
		{
			idList.add( Long.parseLong(deleteIds[i].trim()));//整数数组
		}

	    boolean rsg = stkweightService.removeBatchByIds(idList);
	    if (rsg) {
		    return AjaxResult.success("system.deleteSuccess");
	    }else {
		   return AjaxResult.failed("system.deleteFailed");
	    }
		
	}
	
//	/**
//	 * @explain 查看对象明细
//	 * @param   map 对象参数：id
//	 * @return  JsonResult
//	 * @author  Wonder
//	 */
//	@PostMapping("/detail")
//	@ApiOperation(value = "获取称重申请单对象明细", notes = "作者：Wonder")
//	public AjaxResult<Stkweight> detail(@RequestBody Map<String,Object> map){
//	    if(map==null) return AjaxResult.failed("system.parameterError");
//		Long Id=Long.parseLong(map.get("Id").toString());
//		Stkweight stkweight=stkweightService.getById(Id);
//		if (null!=stkweight ) {
//			 return AjaxResult.success(stkweight);
//		}else {
//			 return AjaxResult.failed("system.parameterError");
//		}
//
//	}
	/**
	 * @param map 对象参数：id
	 * @return JsonResult
	 * @explain 查看子项明细
	 * @author Wonder
	 */
	@ApiOperation(value = "查询stkweight明细")
	@ResponseBody
	@PostMapping(value = "/stkweightdetail")

	public AjaxResult<Map<String, Object>> getStkWeightDetail(HttpServletRequest request, @RequestBody Map<String, Object> map) throws Exception {
		if (null == map){
			return AjaxResult.failed("system.parameterError");
		}
		Map<String,Object> data = stkweightService.getStkWeightDetail(map);
		if (null != data){
			return AjaxResult.success(data);
		} else {
			return AjaxResult.failed();
		}
	}

	/**
	 * 称重申请第一次接口
	 * @param action
	 * @param bh
	 * @return
	 * @throws Exception
	 */
	@ApiOperation(value = "称重申请第一次接口")
	@ResponseBody
	@PostMapping(value = "/fir_purweight")
	public AjaxResult<?> firpurweight(@RequestParam(name = "action") String action, @RequestParam(name = "bh") String bh) throws Exception {
		boolean result = stkweightService.firStkWeight(bh);
		// 构造 form 数据
		Map<String, Object> form = new HashMap<>();
		form.put("bh", 0); // 示例值
		form.put("dm", "");
		form.put("mc", "");
		form.put("menuId", 0);

		// 构造返回数据
		Map<String, Object> response = new HashMap<>();
		response.put("form", form);
		if (result == true) {
			response.put("msg", "操作成功");
		} else {
			response.put("msg", "未查询到数据");
		}
		response.put("result", result);

		return AjaxResult.data(response);
	}

	@ApiOperation(value = "称重申请第二次接口")
	@ResponseBody
	@PostMapping(value = "/sec_purweight")
	public  Map<String,Object> secpurweight(@RequestParam(name = "action") String action, @RequestParam(name = "bh") String bh, @RequestParam(name = "sl") String sl) throws Exception {
		VtuzxMap result = stkweightService.stkSecWeight(bh,sl);
		Map<String, Object> form = new HashMap<>();
		form.put("bh", result.getString("bh")); // 示例值
		form.put("dm", result.getString("BillNo"));
		form.put("mc", "");
		form.put("menuId", 0);

		// 构造返回数据
		Map<String, Object> response = new HashMap<>();
		response.put("form", form);
		response.put("msg", result.getString("msg"));
		response.put("result", result.getString("result"));

		return  response;
	}


	/**
	 * @explain 金蝶称重申请单
	 * @param
	 * @return  Boolean
	 * @author  Wonder
	 */
	@PostMapping("/audit")
	@ApiOperation(value = "审核", notes = "作者：Wonder")
	public AjaxResult<Boolean> audit(@RequestBody JSONObject map) throws Exception{
		if (null != map) {
			boolean res = stkweightService.audit(map);
			if (res){
				return AjaxResult.success(res);
			} else {
				return AjaxResult.failed();
			}
		}else {
			return AjaxResult.failed("system.parameterError");
		}
	}

	/**
	 * @explain 金蝶反称重申请单
	 * @param
	 * @return  Boolean
	 * @author  Wonder
	 */
	@PostMapping("/unaudit")
	@ApiOperation(value = "审核", notes = "作者：Wonder")
	public AjaxResult<Boolean> unAudit(@RequestBody JSONObject map) throws Exception{
		if (null != map) {
			boolean res = stkweightService.unAudit(map);
			if (res){
				return AjaxResult.success(res);
			} else {
				return AjaxResult.failed();
			}
		}else {
			return AjaxResult.failed("system.parameterError");
		}
	}
}